package com.example.springboot.task;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Random;
import java.util.concurrent.Future;

/**
 * @author canger
 * @Deseription 异步与同步调用
 * @create 2018/8/16 15:00
 **/
@Component
public class Task {

    public static Random random=new Random();


    public void toTaskOne() throws InterruptedException {
        System.out.println("开始任务一");
        long startTime = System.currentTimeMillis();
        Thread.sleep(random.nextInt(1000));
        long endTime = System.currentTimeMillis();
        System.out.println("任务一开始消耗:"+(endTime-startTime)+"毫秒");
    }

    public void toTaskSencond() throws InterruptedException {
        System.out.println("开始任务二");
        long startTime = System.currentTimeMillis();
        Thread.sleep(random.nextInt(1000));
        long endTime = System.currentTimeMillis();
        System.out.println("任务二开始消耗:"+(endTime-startTime)+"毫秒");

    }

    public void toTaskThree() throws InterruptedException {
        System.out.println("开始任务san");
        long startTime = System.currentTimeMillis();
        Thread.sleep(random.nextInt(1000));
        long endTime = System.currentTimeMillis();
        System.out.println("任务三开始消耗:"+(endTime-startTime)+"毫秒");
    }
    @Async
    public Future<String> toTaskOnes() throws InterruptedException {
        System.out.println("开始任务一");
        long startTime = System.currentTimeMillis();
        Thread.sleep(random.nextInt(1000));
        long endTime = System.currentTimeMillis();
        System.out.println("任务一开始消耗:"+(endTime-startTime)+"毫秒");
        return new AsyncResult<>("任务一开始消耗:"+(endTime-startTime)+"毫秒");
    }
    @Async
    public Future<String> toTaskSenconds() throws InterruptedException {
        System.out.println("开始任务二");
        long startTime = System.currentTimeMillis();
        Thread.sleep(random.nextInt(1000));
        long endTime = System.currentTimeMillis();
        System.out.println("任务二开始消耗:"+(endTime-startTime)+"毫秒");
        return new AsyncResult<>("任务二开始消耗:"+(endTime-startTime)+"毫秒");
    }
    @Async
    public Future<String> toTaskThrees() throws InterruptedException {
        System.out.println("开始任务san");
        long startTime = System.currentTimeMillis();
        Thread.sleep(random.nextInt(1000));
        long endTime = System.currentTimeMillis();
        System.out.println("任务三开始消耗:"+(endTime-startTime)+"毫秒");
        return new AsyncResult<>("任务三开始消耗:"+(endTime-startTime)+"毫秒");
    }

}
